import styled from "@emotion/styled"
import { useModalSlideUp } from "@/features/useModal"
import { selectedLanguageAtom } from "../../atom"
import { useAtom } from "jotai"
import { SvgComponent } from "@/components/base/Svg"

interface LanguageSelectProps {
    title: string
    options: string[]
}

const LanguageContainer = styled.div`
    width: 100%;
    height: 100%;
    padding: 16rem 16rem 40rem 16rem;
    border-top-right-radius: 10rem;
    border-top-left-radius: 10rem;
    background: rgba(242, 242, 243, 0.95);
    position: relative;
`

const LanguageTitle = styled.div`
    width: 100%;
    height: 28rem;
    line-height: 28rem;
    text-align: center;
    font-family: "Gilroy";
    font-weight: 700;
    font-size: 18rem;
    letter-spacing: 0%;
    text-align: center;
    text-transform: capitalize;
`

const OptionList = styled.div`
    display: flex;
    flex-direction: column;
    gap: 4rem;
    margin-top: 16rem;
`

const OptionItem = styled.div`
    background-color: #ffffff;
    padding: 12rem 16rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-radius: 10rem;
`

const OptionText = styled.div`
    font-family: "Gilroy";
    font-weight: 500;
    font-size: 16rem;
    letter-spacing: 0%;
`

const OptionSelected = styled(SvgComponent("pages/UserCenterPage/selected.svg"))`
    width: 16rem;
    height: 16rem;
`

const OptionClose = styled(SvgComponent("pages/UserCenterPage/cancel_icon.svg"))`
    width: 24rem;
    height: 24rem;
    position: absolute;
    top: 18rem;
    right: 16rem;
`

export default function LanguageSelect({ title, options }: LanguageSelectProps) {
    const [selectedLanguage, setSelectedLanguage] = useAtom(selectedLanguageAtom)
    const handleSelect = (option: string) => {
        setSelectedLanguage(option)
        closeModal()
    }
    const { close: closeModal } = useModalSlideUp("account-page")

    return (
        <LanguageContainer>
            <LanguageTitle>{title}</LanguageTitle>
            <OptionList>
                {options.map((option) => (
                    <OptionItem
                        key={option}
                        onClick={() => handleSelect(option)}
                    >
                        <OptionText>{option}</OptionText>
                        {selectedLanguage === option && <OptionSelected />}
                    </OptionItem>
                ))}
            </OptionList>
            <OptionClose onClick={closeModal}></OptionClose>
        </LanguageContainer>
    )
}
